package cn.fl.system.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.web.core.framework.controller.BaseController;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.CollectionUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.system.constant.SysLogConstant;
import cn.fl.system.service.ISysStdRegionService;
import net.sf.json.JSONArray;

/**
 * @Description: 标准地区表 控制层
 */
@Controller
@RequestMapping("/system/sysstdregion")
public class SysStdRegionController extends BaseController {

	private static final Logger logger = LoggerFactory.getLogger(SysStdRegionController.class);

	/**
	 * SysStdRegion 服务层BEAN
	 */
	@Autowired
	private ISysStdRegionService sysStdRegionService;

	/**
	 * 获取省级数据
	 */
	@RequestMapping(value = "/searchProvinceRegion.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object searchProvinceRegion(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		try {
			JSONArray provinceJSON = this.sysStdRegionService.searchProvinceList();
			if(CollectionUtils.isEmpty(provinceJSON)){
				result.setData(new JSONArray());
			}else{
				result.setData(provinceJSON);
			}
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "searchProvinceRegion", "获取失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "searchProvinceRegion", "获取失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
	/**
	 * 根据区域代码获取子级区域数据
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/searchRegionByRgnPid.do", method = { RequestMethod.POST})
	@ResponseBody
	public Object searchRegionByRgnPid(HttpServletRequest request, HttpServletResponse response) throws BaseException {
		String rgnPid = request.getParameter("rgnPid");
		DataResultVO result = new DataResultVO(BaseConstant.IS_YESNO_YES, SysLogConstant.LOG_CMM_OPERATE_SUCC);
		if(StringUtils.isEmpty(rgnPid)){
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo("获取地域父级ID不存在!");
			return result;
		}
		try {
			JSONArray provinceJSON = this.sysStdRegionService.searchRegionListBy(rgnPid);
			if(CollectionUtils.isEmpty(provinceJSON)){
				result.setData(new JSONArray());
			}else{
				result.setData(provinceJSON);
				BaseLogUtils.info(logger, "searchRegionByRgnPid", "获取数据.", CurrentThreadContext.getCurrentUserName());
			}
		}catch(BaseException ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(ex.getMessage());
			BaseLogUtils.info(logger, "searchRegionByRgnPid", "获取失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}catch(Exception ex) {
			result.setSuccess(BaseConstant.IS_YESNO_NO);
			result.setInfo(SysLogConstant.LOG_CMM_SYS_ERROR);
			BaseLogUtils.error(logger, "searchRegionByRgnPid", "获取失败."+ex.getMessage(), CurrentThreadContext.getCurrentUserName());
		}
		return result;
	}
}

